package me.lijf.xapi.dynamic;

import io.swagger.annotations.Scope;
import lombok.extern.slf4j.Slf4j;
import me.lijf.xapi.config.MybatisConfig;
import me.lijf.xapi.entity.User;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Scope(name = "prototype", description = "")
public class TableNameInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        User user=(User) httpServletRequest.getAttribute("user");
        MybatisConfig.subffix.set(String.valueOf(user.getTenant()));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
